package com.juyu.miliao.api.rabbitMQ;

import com.juyu.miliao.common.constant.QuestionAndAnswerRabbitMqInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author xuanqicheng
 * @create 2018-17-5:49 PM
 */
@Component
@Slf4j
public class QuestionAndAnswerRabbitMq {

    private  final Long DEFAULT_TIME_BY_MILLISECOND_DAY = 86400000L;

    @Autowired
    private AmqpTemplate amqpTemplate;


    /**
     * 设置问答延迟队列，
     * @param message
     */
    public void setQuestionAndAnswerInfo( String delayTime,String message) {

        amqpTemplate.convertAndSend(QuestionAndAnswerRabbitMqInfo.DELAY_EXHANGE,
                QuestionAndAnswerRabbitMqInfo.DELAY_ROUTING_KEY, message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setContentType("application/json");
                if(delayTime==null){
                    message.getMessageProperties().setExpiration(Long.toString(DEFAULT_TIME_BY_MILLISECOND_DAY));
                }else {
                    message.getMessageProperties().setExpiration(delayTime);
                }

                return message;
            }
        });
    }

}
